<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>角色添加</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/layuiadmin/style/admin.css" media="all">
</head>
<body>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-header">角色添加</div>
        <div class="layui-card-body" style="padding: 15px;">
            <form class="layui-form" action="" lay-filter="component-form-group">
                <input hidden name="id">
                <div class="layui-form-item">
                    <label class="layui-form-label">权限名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="authority" lay-verify="required" autocomplete="off" placeholder="请输入权限名称" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">权限描述</label>
                    <div class="layui-input-block">
                        <input type="text" name="name" lay-verify="required" placeholder="请输入权限描述" autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">选择权限</label>
                    <div class="layui-input-block">
                        <div id="LAY-auth-tree-index"></div>
                    </div>
                </div>

                <div class="layui-form-item layui-layout-admin">
                    <div class="layui-input-block">
                        <div class="layui-footer" style="left: 0;">
                            <button class="layui-btn" lay-submit="" lay-filter="component-form-demo1">立即提交</button>
                            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                        </div>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
</body>
<script src="/layuiadmin/layui/layui.js"></script>
<script src="/js/common.js"></script>
<script>
layui.config({
    base: '/layuiadmin/' //静态资源所在路径
}).extend({
    index: 'lib/index', //主入口模块
    authtree: 'authtree/authtree', //主入口模块
}).use(['index','form','authtree','jquery'], function(){

    const form = layui.form,
        authtree = layui.authtree,
    $ = layui.jquery;

    let queryString = getQueryString("id");

    // 获取表单回显数据
    $.ajax({
        url:'/sys-role/one/' + queryString,
        type:'get',
        dataType:'json',
        contentType:'application/json',
        success: function(response){
            if(response.type == 'SUCCESS'){
                // 跳转到登录页
                form.val("component-form-group",response.data);
                return;
            }
            layer.msg(response.message)
            return;
        }
    });


    $.ajax({
        url:'/sys-menu/treeEdit/' + queryString,
        type:'post',
        dataType:'json',
        contentType:'application/json',
        success: function(res){
            var trees = authtree.listConvert(res.data, {
                primaryKey: 'id'
                ,startPid: 0
                ,parentKey: 'pid'
                ,nameKey: 'name'
                ,valueKey: 'id'
                ,checkedKey: 'checked'
            });
            authtree.render('#LAY-auth-tree-index', trees, {
                inputname: 'authids',
                layfilter: 'lay-check-auth',
                autowidth: true,
            });
        }
    });

    /* 监听提交 */
    form.on('submit(component-form-demo1)', function(data){
        let authids = authtree.getChecked('#LAY-auth-tree-index');
        delete(data.field["authids"]);
        data.field.authids = authids;
        console.log(JSON.stringify(data.field));
        $.ajax({
            url:'/sys-role/update',
            type:'post',
            dataType:'json',
            data:JSON.stringify(data.field),
            contentType:'application/json',
            success:function(response){
                if(response.type == 'SUCCESS'){
                    layer.msg("更新成功");
                    let index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                    parent.layer.close(index);//关闭弹出的子页面窗口
                    return;
                }
                layer.msg(response.message)
            },
            error: function(msg){

            }
        });
        return false;
    });

});
</script>
</html>
